import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author Macrydy
 * @date 2022/5/9
 *
 * ArrayList
 * 特性：存取有序、可重复、有索引
 * 删除元素最好使用迭代器
 * 可使用增强for循环
 *
 * 排序：1.Student类实现Comparable接口重写compareTo方法
 *      2.ArrayList构造方法传入Comparator接口重写compareTo方法(推荐)
 */
public class ArrayListDemo {
    public static void main(String[] args) {

        List<Student> list = new ArrayList<>();

        //添加元素
        list.add(new Student("xiaoming",18));
        list.add(new Student("xiaoming",23));
        list.add(new Student("xiaobai",20));
        list.add(new Student("xiaohong",19));

        System.out.println(list);

        //普通for循环遍历
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }

        //使用迭代器遍历
        Iterator<Student> iterator = list.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

        //增强for循环
        for (Student student : list) {
            System.out.println(student);
        }

        //使用迭代器删除元素
        Iterator<Student> iterator1 = list.iterator();
        while (iterator1.hasNext()) {
            if (iterator1.next().getName().equals("xiaobai")){
                iterator1.remove();
            }
        }
        System.out.println(list);

    }
}
